<?php
/**
 * Created by PhpStorm.
 * User: hebidu
 * Date: 15/11/19
 * Time: 15:04
 */

namespace Api\Service;


class AliBaichuanService {



    public function sendMessage($mobile,$context=array()){

        vendor('TopSdk',APP_PATH.'Api/Vendor/Alibaichuan/','.php');
        $domain = $context['domain'];
        $signature_id = $context['signature_id'];
        $template_id = $context['template_id'];

        if(!empty($domain)){
            $domain = "unknown";
        }

        $c = new \TopClient();
        $c->appkey = $context['appkey'];
        $c->secretKey = $context['secretKey'];

        $c->format = "json";
        $req = new \OpenSmsSendmsgRequest;
        $send_message_request = new \SendMessageRequest();
        $send_message_request->mobile_limit="5";  //5次 mobile_limit_in_time内5次
        $send_message_request->external_id="12345";
        $send_message_request->mobile_limit_in_time="3600";//1小时
        $send_message_request->template_id= $template_id;
        $send_message_request->signature_id= $signature_id;
        $send_message_request->domain = $domain;
        $send_message_request->mobile=$mobile;
        $send_message_request->context =$context;
        $req->setSendMessageRequest(json_encode($send_message_request));
        $resp = $c->execute($req);
        if(isset($resp['result'])){
            //返回信息
            $result = $resp['result'];
            if(isset($result['code']) && $result['code'] == 1 && $result['successful']){
                return array('status'=>true,'info'=>"短信已发送,请注意查收!");
            }else{
                return array('status'=>false,'info'=>"短信发送失败,请重试!",'_result'=>$result);
            }

        }else{
            //发生异常
            if(isset($resp['sub_msg'])){
                return array('status'=>false,'info'=>$resp['sub_msg']);
            }else{
                return array('status'=>false,'info'=>$resp['msg']);
            }
        }



    }

    //**针对商城的业务场景 发送短消息***********************************

    /**
     * 发送订单确认消息
     * @param string $mobile 手机号
     * @param string $order_code 订单号
     * @param string $date_time 时间
     * @param string $who
     * @return array
     */
    public function confirmOrderMsg($mobile,$order_code,$date_time,$who='卖家'){

        $domain=  "order";

        $context = array(
            'order_code'=>$order_code,
            'who'=>$who,
            'date_time'=>$date_time,

            'domain'=>$domain,
            'signature_id'=>'761',
            'template_id'=>'1580',
            'appkey'=>'23271070',
            'secretKey'=>'4c8565ef1d6904eaebc435f02977f73d',
        );

        $result = $this->sendMessage($mobile,$context);

        return $result;

    }


    /**
     * 订单发货
     * @param string $mobile 手机号
     * @param string $order_code 订单编号
     * @param string $express_company 快递公司
     * @param string $express_code 快递单号
     * @return array
     */
    public function deliverOrderMsg($mobile,$order_code,$express_company,$express_code){

        $domain=  "order";

        $context = array(
            'express_company'=>$express_company,
            'express_code'=>$express_code,
            'order_code'=>$order_code,
            'date_time'=>date("Y-m-d H:i:s",time()),

            'domain'=>$domain,
            'signature_id'=>'761',
            'template_id'=>'1581',
            'appkey'=>'23271070',
            'secretKey'=>'4c8565ef1d6904eaebc435f02977f73d',
        );

        $result = $this->sendMessage($mobile,$context);

        return $result;
    }



}